package cn.com.xy.sms.sdk.service.pubInfo;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.util.Log;
import cn.com.xy.sms.sdk.util.StringUtils;

public class PublicInfoCacheManager {
    
    private static final String TAG = "PublicInfoCacheManager";
    
    public static HashMap<String, JSONObject> publicInfoData = new HashMap<String, JSONObject>();

    public static HashMap<String, String[]> phonePublicIdData = new HashMap<String, String[]>();
    
    public static void putAllToPhonePublicIdData(HashMap<String, String[]> numResult){
        if(numResult!=null&&!numResult.isEmpty()){
            synchronized (phonePublicIdData) {
                phonePublicIdData.putAll(numResult);
            }
        }
    }
    
    public static void putAllTopublicInfoData(HashMap<String, JSONObject> result){
        if(result!=null&&!result.isEmpty()){
            synchronized (publicInfoData) {
                publicInfoData.putAll(result);
            }
        }
    }
    
    public static void updatePublicInfoDataCache(JSONObject res) {
        if (res == null) {
            return;
        }
        if (res.has("pubId")) {
            String key = res.optString("pubId");
            JSONObject pubInfo = getPubInfoCacheDataFromJSB(res);

            String phoneNum = "";
            String purpose = "";

            if (pubInfo != null && pubInfo.has("num")) {
                phoneNum = pubInfo.optString("num");
            }
            if (pubInfo != null && pubInfo.has("purpose")) {
                purpose = pubInfo.optString("purpose");
            }
            
            if(!StringUtils.isNull(phoneNum) && phonePublicIdData.containsKey(phoneNum)){
                synchronized (publicInfoData) {
                    publicInfoData.put(key, pubInfo);
                }
                synchronized (phonePublicIdData) {
                    phonePublicIdData.put(phoneNum, new String[] { key, purpose });
                }
            }
        }
    }
    
    public static JSONObject getPubInfoCacheDataFromJSB(JSONObject res){
        if (res == null) {
            return null;
        }
        try {
            String key = "";
                key = res.optString("pubId");
                String phoneNum = "";
                String purpose = "";
                
                JSONObject pubInfo = new JSONObject();
                pubInfo.put("id", key);
                if (res.has("pubName")) {
                    pubInfo.put("name", res.optString("pubName"));
                }
                if (res.has("classifyCode")) {
                    pubInfo.put("classifyCode", res.optString("classifyCode"));
                }
                if (res.has("logoType")) {
                    pubInfo.put("logoType", res.optString("logoType"));
                }
                if (res.has("rectLogoName")) {
                    pubInfo.put("logo", res.optString("rectLogoName"));
                }
                if (res.has("circleLogoName")) {
                    pubInfo.put("logoc", res.optString("circleLogoName"));
                }
                if (res.has("scale")) {
                    pubInfo.put("scale", res.optString("scale"));
                }
                if (res.has("backColor")) {
                    pubInfo.put("backColor", res.optString("backColor"));
                }
                if (res.has("backColorEnd")) {
                    pubInfo.put("backColorEnd", res.optString("backColorEnd"));
                }
                if (res.has("pubNumInfolist")) {
                    JSONObject pubNumInfo = res.optJSONArray("pubNumInfolist").getJSONObject(0);
                    
                    if (pubNumInfo != null && pubNumInfo.has("num")) {
                        phoneNum = pubNumInfo.optString("num");
                        pubInfo.put("num", phoneNum);
                    }
                    if (pubNumInfo != null && pubNumInfo.has("purpose")) {
                        purpose = pubNumInfo.optString("purpose");
                        pubInfo.put("purpose", purpose);
                    }
                }
                return pubInfo;
            
        } catch (Throwable e) {
        }
        return null;
    }
    
    public static JSONObject getPublicInfoByPhoneIncache(String phone) {
        if (StringUtils.isNull(phone))
            return null;
        JSONObject json = null;
        String[] phoneValues = phonePublicIdData.get(phone);
        String purpose = "";
        if (phoneValues!=null && phoneValues.length>0) {
            String publicId = phoneValues[0];
            if(phoneValues.length>1){
                purpose = phoneValues[1];
            }
            json = publicInfoData.get(publicId);
        }
        if (json!=null && !StringUtils.isNull(purpose)) {
            try {
                json.put("purpose", purpose);
            }
            catch (Throwable e) {
            }
        }        
        return json;
    }
    
    public static JSONObject getPublicInfoById(String pubId){
       if (StringUtils.isNull(pubId)) {
        return null;
       }
       return publicInfoData.get(pubId);
    }
    
    /**
     * 
     * @param jsonStr
     * @param phoneNum
     * @param extend
     *  当extend有参数addCache且值为字符串1时，代表存入SDK缓存；extend为空或值不为1则不使用
     */
    public static void saveJsonToCache(String jsonStr, String phoneNum, Map<String, String> extend){
        try {
            if(StringUtils.isNull(jsonStr) || StringUtils.isNull(phoneNum))
                return; 
            if (extend==null || !"1".equals((String)extend.get("addCache"))) 
                return;
            
            JSONObject json = new JSONObject(jsonStr);
            String publicId = json.optString("id");         
            json.remove("classifyName");
            json.remove("classifyCode");
            json.remove("email");
            json.remove("weiboName");
            json.remove("weiboUrl");
            json.remove("weixin");
            json.remove("website");
            json.remove("moveWebSite");
            json.remove("pubnum");
            if (!StringUtils.isNull(publicId)) {
                synchronized (publicInfoData) {
                    publicInfoData.put(publicId, json);
                }
                String[] numValues = {publicId};
                synchronized (phonePublicIdData) {
                    phonePublicIdData.put(phoneNum, numValues);
                }
            }       
        } catch (Throwable e) {
            e.printStackTrace();
        }
    
    }
}
